package com.oig.sys.auth.util;

import cn.hutool.core.util.ClassUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;


public class FreemarkerUtil {

    private static Configuration freemarkerCfg = null;

    static {
        freemarkerCfg =new Configuration(Configuration.VERSION_2_3_30);
        //freemarker的模板目录
        try {
            freemarkerCfg.setDirectoryForTemplateLoading(new File(ClassUtil.getClassPath()+"templates"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static String parseTpl(String ftlName, Map<String, Object> params) {
        String ftlText = null;
        try {
            Template t =  freemarkerCfg.getTemplate(ftlName);
            ftlText = FreeMarkerTemplateUtils.processTemplateIntoString(t, params);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
        return ftlText;
    }


}
